from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

#from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket

import uuid


def myindex(request):
    return HttpResponse('这里是首页')

def hello(request):
    return HttpResponse('HelloWorld')

# 作业部分
from .models import User
# 基础展示
class MyView(View):
    def get(self,request):
        user_list = User.objects.all()
        return render(request, 'index.html', locals())
# 修改
class UpdateView(View):
    def get(self, request, id):
        user = User.objects.get(id=id)
        return render(request, 'update.html', locals())
    
def UpdatePost(request):
    request.encoding = 'utf-8'
    user = User.objects.get(id=request.POST.get('ID'))
    user.username = request.POST.get('username')
    user.password = request.POST.get('password')
    user.img = request.POST.get('img')
    user.type = request.POST.get('type')
    user.save()
    return redirect('http://127.0.0.1:8000/')

# 删除
def DeleteView(request, id):
    User.objects.get(id=id).delete()
    print('删除成功')
    return redirect('http://127.0.0.1:8000/')
# 创建
class CreateView(View):
    def get(self, request):
        return render(request, 'create.html', locals())
    def post(self, request):
        request.encoding = 'utf-8'
        username = request.POST.get('username')
        password = request.POST.get('password')
        img = request.POST.get('img')
        type = request.POST.get('type')
        user = User()
        user.username = username
        user.password = password
        user.img = img
        user.type = type
        user.save()
        return redirect('http://127.0.0.1:8000/')



